package cc.fs.kotlinproject.network

import android.os.AsyncTask
import android.util.Log
import com.squareup.okhttp.OkHttpClient
import com.squareup.okhttp.Request
import com.squareup.okhttp.Response
import java.util.concurrent.TimeUnit

/**
 * Created by fostion on 2015/8/11.
 */

public class OkHttpUtil {

    companion object{
        val connect_timeout: Long = 5000
        var client: OkHttpClient? = null

        init {
            client = OkHttpClient();
            client?.setConnectTimeout(connect_timeout, TimeUnit.SECONDS)
            client?.setWriteTimeout(connect_timeout, TimeUnit.SECONDS)
            client?.setReadTimeout(connect_timeout, TimeUnit.SECONDS)
        }

        fun get(url: String) {
            val request: Request = Request.Builder().url(url).build()
            val response: Response? = client!!.newCall(request).execute()
            if (response?.isSuccessful() ?: false) {
                Log.e("--- ", " " + response?.code() + "  -- " + response?.body().toString())
                return
            }
        }

        fun get(url: String, callback: ((String) -> Unit)?) {
            try {
                val request: Request = Request.Builder().url(url).build()
                val response: Response? = client!!.newCall(request).execute()
                if (response?.isSuccessful() ?: false) {
                    callback?.invoke(" " + response?.code())
                    return
                }
            } finally {
            }
        }
    }

}
